using System.Runtime.CompilerServices;
using LP;
using UnityEngine;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;

public class GameConfigMgr : AbsMgr
{
    private List<string> configNames = new List<string>();
    private Dictionary<string, JObject> configs = new Dictionary<string, JObject>();

    public override void OnInit()
    {
        base.OnInit();
        IsUpdate = false;
        Global.SysEventHandler.RegHandler(ESys.LoadGameConfigs, LoadGameConfigs, TokenSource.Token);
        InitConfigNames();
    }


    private void InitConfigNames()
    {
    }

    private async void LoadGameConfigs(ITuple tuple)
    {
        configs.Clear();
        int initCount = 0;
        Debug.Log("正在加载游戏配置...");

        foreach (var item in configNames)
        {
            var config = await GetGameConfig(item);
            initCount++;
            configs.Add(item, config);
            float radiot = ((float) initCount / configNames.Count) * 0.2f + 0.8f;
            Debug.Log("加载进度配置进度" + radiot);
        }

        Debug.Log("配置表加载完成");
    }

    /// <summary>
    /// 获取游戏配置
    /// </summary>
    /// <param name="configName">游戏配置名称(不需要加扩展名)</param>
    private async Task<JObject> GetGameConfig(string configName)
    {
        string path = $"GameConfig/{configName}.json.txt";
        var task = Global.ResMgrIns.LoadAssetAsyncTask<TextAsset>(path);
        await task;
        return JObject.Parse(task.Result.text);
    }

    public JObject GetConfig(string configName)
    {
        return configs[configName];
    }
}